home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / pctchnqs / 1990 / number5 / serial.hpp < prev    next >
C/C++ Source or Header  |  1990-10-06  |  6KB  |  143 lines

  1. /**************************************************************************
  2. *   SERIAL.HPP - Listing 1
  3. *   Written by Kevin D. Weeks, August 1990
  4. *   Compiles and runs under Borland Turbo C++ and Zortech C++.
  5. */
  6. #if !defined(COMM_HPP)
  7. #define COMM_HPP
  8. // the following enum types perform two functions. first, they are checked
  9. // for type by the compiler thus preventing an invalid parameter from
  10. // being passed. second, they function as indices into the tables defined
  11. // at the end of the class definition
  12. enum Com_Port
  13.     { Com_1, Com_2 };
  14. enum Baud_Rate
  15.     {   Baud_110, Baud_150, Baud_300, Baud_600, Baud_1200, Baud_2400,
  16.         Baud_4800, Baud_9600 };
  17. enum Parity
  18.     { Odd_Parity, Even_Parity, No_Parity };
  19. enum Stop_Bits
  20.     {   Stop_Bits_1, Stop_Bits_2 };
  21. enum Data_Bits
  22.     {   Data_Bits_7, Data_Bits_8 };
  23. enum Bool
  24.     {   FALSE, TRUE };
  25. enum Result
  26.     {   OK, ERROR = -1 };
  27. // define a hand-shaking function type
  28. typedef   int(*HAND_SHAKE)(int);
  29. // assembly function prototypes
  30. extern "C"
  31. {
  32.     Result  com_open(int port, unsigned int paramters);
  33.     void    com_close(void);
  34.     int     com_read(void *buffer);
  35.     Result  com_write(int num_bytes, void *buffer);
  36.     int     com_read_char(void);
  37.     Result  com_write_char(unsigned char chr);
  38.     void    com_start_sending(void);
  39.     void    com_stop_sending(void);
  40.     unsigned int    com_get_status(void);
  41.     int     com_chars_recvd(void);
  42.     int     com_chars_sent(void);
  43.     void    com_set_buffers(unsigned char *recv_buffer,
  44.                             unsigned char *send_buffer, int length);
  45.     void    com_set_handshake(HAND_SHAKE);
  46.     void    com_clr_recv_buf(void);
  47.     void    com_clr_send_buf(void);
  48. };
  49. // define the bit flags returned by com_get_status(). note that bits
  50. // 0 - 3 indicate current status, 4 - 7 indicate line errors, and 8 -
  51. // 10 indicate execution errors
  52. typedef union
  53. {
  54.     unsigned int    value;
  55.     struct
  56.     {
  57.         unsigned    port_open : 1;
  58.         unsigned    char_received : 1;
  59.         unsigned    sending_message : 1;
  60.         unsigned    hand_shaking : 1;
  61.         unsigned    overrun_err : 1;
  62.         unsigned    parity_err : 1;
  63.         unsigned    framing_err : 1;
  64.         unsigned    break_received : 1;
  65.         unsigned    buffer_overflow : 1;
  66.         unsigned    invalid_port : 1;
  67.         unsigned    port_not_found : 1;
  68.     } flag;
  69. } Comm_Status;
  70. #define DEFAULT_BUF_SIZE    1024
  71. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  72. *  the methods marked with a "// *" will automatically return an error
  73. *  unless the port is owned by the current instance of the class    */
  74. class Serial_Comm
  75. {
  76.   public:
  77.     // constructors
  78.         Serial_Comm(void);
  79.         Serial_Comm(Com_Port port, Baud_Rate baud, Parity par,
  80.                     Stop_Bits stop, Data_Bits data);
  81.     // destructor
  82.         ~Serial_Comm(void);
  83.     // the first group of methods provide for actually accessing data
  84.     // they will return an ERROR or -1 if the port is unowned or is
  85.     // owned by another instance of the class.
  86.     Result          open(void);
  87.     void            close(void);
  88.     int             read(void *client_buffer);
  89.     Result          write(void *client_buffer, int length);
  90.     int             read_char(void);
  91.     Result          write_char(unsigned char);
  92.     // the next two methods allow starting over. note that clr_send_
  93.     // buffer will disable sending if sending is enabled. they will
  94.     // return an ERROR if another instance owns the port
  95.     Result          clr_recv_buffer(void);
  96.     Result          clr_send_buffer(void);
  97.     // get_status() returns a 0 if the port is owned by another.
  98.     Comm_Status     get_status(void);
  99.     // the next two methods return an ERROR if the port is owned
  100.     // by another instance. in both caes 0 is a valid return value.
  101.     int             get_bytes_recvd(void);
  102.     int             get_bytes_sent(void);
  103.     // all of the remaining methods will function whether the instance
  104.     // in question owns the port or not.
  105.     Com_Port        get_port(void) { return com_port; };
  106.     Baud_Rate       get_baud_rate(void) { return baud_rate; };
  107.     Parity          get_parity(void) { return parity; };
  108.     Stop_Bits       get_stop_bits(void) { return stop_bits; };
  109.     Data_Bits       get_data_bits(void) { return data_bits; };
  110.     int             get_buffer_size(void) { return buffer_size; };
  111.     // if the port is open AND owned by the particular instance called
  112.     // then it will be closed before the parameters are changed and
  113.     // then re-opened. since closing the port will cause anything in
  114.     // the send or receive buffers to be discarded these methods
  115.     // should be used carefully.
  116.     void            set_port(Com_Port port);
  117.     void            set_baud_rate(Baud_Rate baud);
  118.     void            set_parity(Parity par);
  119.     void            set_stop_bits(Stop_Bits stop);
  120.     void            set_data_bits(Data_Bits data);
  121.     Result          set_buffer_size(int size);
  122.   protected:
  123.   // the following attributes are shared by all instances of the class
  124.   // and descendants.
  125.     static Serial_Comm      *open_flag;
  126.     static unsigned int     baud_table[8];
  127.     static unsigned int     parity_table[3];
  128.     static unsigned int     stop_table[2];
  129.     static unsigned int     data_table[2];
  130.   private:
  131.   // all of these attributes are accessable through methods so keep
  132.   // them private.
  133.     int             buffer_size;
  134.     unsigned char   *recv_buffer;
  135.     unsigned char   *send_buffer;
  136.     Com_Port        com_port;
  137.     Baud_Rate       baud_rate;
  138.     Parity          parity;
  139.     Stop_Bits       stop_bits;
  140.     Data_Bits       data_bits;
  141. };
  142. #endif
  143.